﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTO;

namespace IndexMarkWcfService.Caches
{
    public class PeriodCache: CacheInvokerBase
    {
        private static IList<Period> cacheList = new List<Period>();

        public PeriodCache()
        {

        }

        public override object[] AllocateInputs()
        {
            return BaseInvoker.AllocateInputs();
        }

        public override object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            IEnumerable<Period> result = new List<Period>();

            if (cacheList.Count() > 0)
            {
                result = cacheList;
            }
            else
            {
                result = BaseInvoker.Invoke(instance, inputs, out outputs) as IEnumerable<Period>;
                cacheList = result.ToList();
            }

            outputs = new object[] { };
            return result;
        }

        public override IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            return BaseInvoker.InvokeBegin(instance, inputs, callback, state);
        }

        public override object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            return BaseInvoker.InvokeEnd(instance, out outputs, result);
        }

        public override bool IsSynchronous
        {
            get { return true; }
        }

        public static void Clear()
        {
            cacheList.Clear();
        }
    }
}
